package sjtu.ist.warehouse4ws.service;

import java.util.ArrayList;
import java.util.List;

import sjtu.ist.warehouse4ws.dao.EpcunitsDao;
import sjtu.ist.warehouse4ws.dao.cust.CustEpcunitsDao;
import sjtu.ist.warehouse4ws.model.CustEpcunits;
import sjtu.ist.warehouse4ws.model.Epcunits;
import sjtu.ist.warehouse4ws.tool.Constants;
import sjtu.ist.warehouse4ws.tool.PropertyHelper;


public class EpcunitsServiceImpl implements EpcunitsService{
	
    private EpcunitsDao epcunitsDao;
    private CustEpcunitsDao custEpcunitsDao;
	
	public void setEpcunitsDao(EpcunitsDao epcunitsDao) {
		this.epcunitsDao = epcunitsDao;
	}


	public void setCustEpcunitsDao(CustEpcunitsDao custEpcunitsDao) {
		this.custEpcunitsDao = custEpcunitsDao;
	}


	@Override
	public List<Epcunits> getEpcunitsList() {
		// TODO Auto-generated method stub
		return (List<Epcunits>)epcunitsDao.findAllEpcunits();
	}


	@Override
	public void saveEpcUnits(Epcunits epcUnits, int companyId) {
		// TODO Auto-generated method stub
		if(companyId==Constants.ADMIN_COMPANY_ID){
			this.epcunitsDao.save(epcUnits);
		}else{
			CustEpcunits ce=new CustEpcunits();
			PropertyHelper.copyProperties(epcUnits, ce);
			custEpcunitsDao.save(ce);
		}		
	}


	@Override
	public List<Epcunits> getEpcunitsListById(String id) {
		// TODO Auto-generated method stub
		return epcunitsDao.findById(id);
	}


	@Override
	public Epcunits getEpc(String id) {
		// TODO Auto-generated method stub
		return epcunitsDao.getEpc(id);
	}


	@Override
	public List<Epcunits> getEpcunitsListByGroupId(Integer groupId, int companyId) {
		// TODO Auto-generated method stub
		if(companyId==Constants.ADMIN_COMPANY_ID){
			return epcunitsDao.findAllEpcunitsByGroupId(groupId);
		}else{
			List<CustEpcunits> clist = custEpcunitsDao.findByGroupId(groupId);
			List<Epcunits> list=new ArrayList<Epcunits>();
			for(CustEpcunits ce : clist){
				Epcunits e=new Epcunits();
				PropertyHelper.copyProperties(ce, e);
				list.add(e);				
			}
			return list;
		}
		
	}

	@Override
	public List<Epcunits> getEpcunitsListByGroupAndVersion(String groupId,String versionId,int companyId) {
		// TODO Auto-generated method stub
		if(companyId==Constants.ADMIN_COMPANY_ID){
			return epcunitsDao.findAllEpcunitsByGroupAndVersion(groupId,versionId);
		}else{
			List<Epcunits> list=new ArrayList<Epcunits>();
			List<CustEpcunits> clist=custEpcunitsDao.findByGroupAndVersion(groupId,versionId);
			for(CustEpcunits ce : clist){
				Epcunits e=new Epcunits();
				PropertyHelper.copyProperties(ce, e);
				list.add(e);
			}
			return list;
		}
		
	}

}
